//-----------------------------------------------------------------------
// <copyright file="GenericParameter.cs" company="iron9light">
//      Copyright (c) iron9light. All rights reserved.
// </copyright>
// <author>iron9light</author>
//-----------------------------------------------------------------------

namespace EventBrokerExtension
{
    using System;

    /// <summary>
    /// A <see cref="MethodParameter"/> that lets you specify that
    /// an instance of a generic type parameter.
    /// </summary>
    public class GenericParameter : MethodParameter
    {
        private readonly string genericParameterName;

        /// <summary>
        /// Initializes a new instance of the <see cref="GenericParameter"/> class
        ///  that specifies that the given named generic parameter.
        /// </summary>
        /// <param name="genericParameterName">The generic parameter name.</param>
        public GenericParameter(string genericParameterName)
        {
            this.genericParameterName = genericParameterName;
        }

        /// <summary>
        /// Gets name for the type represented by this <see cref="MethodParameter"/>.
        /// This may be an actual type name or a generic argument name.
        /// </summary>
        /// <value>The name of the parameter type.</value>
        public override string ParameterTypeName
        {
            get { return this.genericParameterName; }
        }

        /// <summary>
        /// Test to see if this parameter value has a matching type for the given type.
        /// </summary>
        /// <param name="type">Type to check.</param>
        /// <returns>True if this parameter value is compatible with type <paramref name="type"/>,
        /// false if not.</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods",
                Justification = "Validation done by Guard class")]
        public override bool MatchesType(Type type)
        {
            Guard.ArgumentNotNull(type, "type");
            return type.IsGenericParameter && type.Name == this.genericParameterName;
        }
    }
}